package com.example.helloworld;

/*
First data is read in then parsed into params.
A request is made to 
*/

import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.*;
 
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

@SuppressWarnings("javadoc")
public class NewBetActivity extends Activity {
	
	// Progress Dialog
	private ProgressDialog pDialog;

	// JSON Parser object
	JSONParser jParser = new JSONParser();
	EditText input_bettorID;
    EditText input_value;
	
	// URL
	private static String url_root = "http://rsl.lib.fit.edu/android_connect/";
	private static String url_createBet = url_root + "bet_create.php";

	// JSON nodes
	private static final String TAG_SUCCESS = "success";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_bet);
		
		// Edit text
		this.input_bettorID = (EditText) findViewById(R.id.bettorID);
		this.input_value = (EditText) findViewById(R.id.value);
		
		// Create button
		Button btnAddNewBet = (Button) findViewById(R.id.btnAddNewBet);
		
		// Button click event
		btnAddNewBet.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View view) {
				new AddNewBet().execute();
			}
		});
	}
	
	// Background async task to add new bet
	class AddNewBet extends AsyncTask<String, String, String> {
		// Show progress dialog.
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			NewBetActivity.this.pDialog = new ProgressDialog(NewBetActivity.this);
			NewBetActivity.this.pDialog.setMessage("Placing bet...");
			NewBetActivity.this.pDialog.setIndeterminate(false);
			NewBetActivity.this.pDialog.setCancelable(true);
			NewBetActivity.this.pDialog.show();
		}
		
		// Create bet
		@Override
		protected String doInBackground(String ...args) {
			String bettorID = NewBetActivity.this.input_bettorID.getText().toString();
			String value = NewBetActivity.this.input_value.getText().toString();
			
			// Build the params
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("bettorID", bettorID));
			params.add(new BasicNameValuePair("value", value));
			
			// getting the JSON object
			// bet_create accepts the post method
			JSONObject json = NewBetActivity.this.jParser.makeHttpRequest(url_createBet, 
				"POST", params);
			
			// check log cat for the response
			Log.d("Create Response", json.toString());
			
			// Check for success.
			try {
				int success = json.getInt(TAG_SUCCESS);
				
				if (success == 1) {
					// bet was placed
					Intent i = new Intent(getApplicationContext(), LoadAllBetsActivity.class);
					startActivity(i);
					
					// close this screen
					finish();
				} else {
					// failed to make bet
					// freak out accordingly
					// should never happen
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			return null;
		}
	
		// After finishing background task, dismiss progress dialog.
		@Override
		protected void onPostExecute(String file_url) {
			NewBetActivity.this.pDialog.dismiss();
		}
	}
}